//
//  AppDelegate.m
//  01-UIView UIWindow
//
//  Created by mafeng on 16/7/14.
//  Copyright © 2016年 mafeng. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "UIView+Frame.h"
@interface AppDelegate () <UITextFieldDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    // 程序的组成结构
    // 1. main.m 主要实现了程序的正常运行
    // 2. AppDelegate 程序的执行者, 签订了UIApplicationDelegate(协议方法可选)
    // 注意: AppDelegate就是一个普通的类, 只不过签订了协议, 才有了这么多方法
    // 3. ViewController 视图控制器 主要负责视图管理 看得见,摸得到
    // main.sb(视图管理) LaunchScreen.sb(负责启动页) 可视化管理(Xcode 7 以前 LaunchScreen.xib)
    // 5. Assets.xcassets 主要用来管理图片素材(Xcode 7 以前叫这个 Images.xcassets)
    // 6. info.plist (工程配置文件)
    
    // 模拟器
    // command + 1~5 改变模拟器的大小
    // command + ⬅️ / → 横 / 竖 屏切换
    // iOS 概述
    
    // 1. UIWindow(程序中只有一个主window)
    //  1> 取消程序的可视化入口, main.sb
    //  2> 创建主window对象
    // 创建 主 window 使其大小和屏幕一样
    // 1. UIScreen 系统屏幕类
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    // 2. 给window填颜色
    _window.backgroundColor = [UIColor yellowColor];
    // 3. 让window 成为主窗口 并 显示
    [_window makeKeyAndVisible];
    //
    // 4. Xcode 7 以后, 必须设置rootViewController, 否则, 在该方法结束时, 会崩溃
    _window.rootViewController = [[ViewController alloc] init];
    
    //  3> UIWindow 继承于 UIView
    
    // 2. UIView(与NSObject相似, 是UI阶段的根类, 大部分UI控件继承于UIView)
    // 学习新类:
    //   1> 新类包含哪些初始化方法
    //   2> 包含哪些行为(方法)和特征(属性)
#if 0
    //  创建UIView
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 250)];
    // 设置背景色
    view.backgroundColor = [UIColor greenColor];
    // 添加到window上
    [_window addSubview:view];
    // 遇到UIView的子类, 都可以用上面的方式创建和显示
    // UIView 在屏幕中显示为矩形
    // 坐标系
    // 1. 与数学中的坐标系不同, y的正方向是向下的
    // 2. 原点是屏幕的左上角
    
    // frame bounds center (这三个都是UIView的属性)
    // 1. frame (x, y, width, height) 视图在其父视图上的位置
    // 2. bounds (x, y, width, height) 视图本身的坐标系 (0, 0, width, height)
    // bounds 的 x, 和 y, 修改本身的坐标系, 影响他的子视图
    // bounds 的 width, height修改的是本身大小, 会以中心店收缩, 扩张.
    // bounds的改变不会改变该视图的位置
    // 3. center 中心点
    // view.frame = CGRectMake(10, 10, 80, 80);
    // bounds 对视图的影响
    // bounds 的 x, y 影响子视图的frame, 改变坐标系
    // CGRect 和 CGPoint 
    NSLog(@"改变bounds前:%@", NSStringFromCGRect(view.frame));
    NSLog(@"改变前的center:%@", NSStringFromCGPoint(view.center));
    view.bounds = CGRectMake(10, 10, 100, 100);
     NSLog(@"改变bounds后:%@", NSStringFromCGRect(view.frame));
     NSLog(@"改变后的center:%@", NSStringFromCGPoint(view.center));
    UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    subView.backgroundColor = [UIColor blueColor];
    [view addSubview:subView];
    
    // 改变绿色view的大小, 变成 100
    view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, 100, view.frame.size.height);
    //  view.frame 是getter方法
    //  frame.size 结构体
    //  OC和C的内容的结合, 是一个常量, 不能被赋值
   // view.frame.size.width = 100;
    CGRect rect = view.frame;
    rect.size.width = 100;  // 这里只有C语言,没有OC的参与, 不是常量, 所以可以赋值
    view.frame = rect;
    
    view.mf_height = 100;
#endif
    // UIView的基本属性
    UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    // 1. 设置背景颜色
    testView.backgroundColor = [UIColor cyanColor];
    // 添加
    [_window addSubview:testView];
    // 移除
    //[testView removeFromSuperview];
    // 2. 透明度 0.0 ~ 1.0
    testView.alpha = 0.5;
    // 3 tag 标记(编号) 默认为0
    testView.tag = 1000;
    // 根据tag获取到对应的view  __kindof
    UIView *view = [_window viewWithTag:1000];
    // 4. subviews 子视图们
    UIView *arrayView = [_window.subviews objectAtIndex:0];
    NSLog(@"%@", arrayView);
    // 5. supperView 父视图
    // testView.superview;
    // hidden 显示/隐藏   NO / YES
    testView.hidden = YES;
    
    // 视图层级
    UIView *firstView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
    firstView.backgroundColor = [UIColor blueColor];
    UIView *secondView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 80, 80)];
    secondView.backgroundColor = [UIColor grayColor];
    UIView *lastView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 80, 80)];
    lastView.backgroundColor = [UIColor cyanColor];
    // testView addSubview:<#(nonnull UIView *)#>
    // 添加视图
    [_window addSubview:firstView];
    // testView insertSubview:<#(nonnull UIView *)#> atIndex:<#(NSInteger)#>
    // 插入视图到指定下标
    [_window insertSubview:secondView atIndex:2];
    NSLog(@"%@", _window.subviews);
    // testView insertSubview:<#(nonnull UIView *)#> aboveSubview:<#(nonnull UIView *)#>
    // 插入视图到指定视图的上面
    [_window insertSubview:lastView aboveSubview:secondView];
    // 插入视图到指定视图的下面
    // testView insertSubview:<#(nonnull UIView *)#> belowSubview:<#(nonnull UIView *)#>
    
    // UILabel(负责显示文本)
    // 如果没有自己的初始化方法, 则用父类的初始化方法
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 300, 100)];
    label.backgroundColor = [UIColor cyanColor];
    [_window addSubview:label];
    label.text = @"aifish.top aifdsaf  fsa i am zai ma shfisf sfds ";
    // 2. lineBreakMode 换行模式, ...位置(中间, 左边, 右边)
    label.lineBreakMode = NSLineBreakByClipping;
    label.textColor = [UIColor whiteColor];
    // 3. font 默认值 17
    label.font = [UIFont systemFontOfSize:100];
    label.font = [UIFont boldSystemFontOfSize:15];
    // 4. textAlignment 对齐方式 (默认对齐方式 left)
    label.textAlignment = NSTextAlignmentNatural;
    // 5. 行数 不确定行数时, 给0
    label.numberOfLines = 3;
    label.numberOfLines = 0;
    // 6. 阴影
    label.shadowColor = [UIColor blackColor];
    label.shadowOffset = CGSizeMake(-2, 0);
    // UIButton
    // UIButtonTypeSystem 系统的按钮类型
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.backgroundColor = [UIColor purpleColor];
    button.frame = CGRectMake(200, 20, 200, 40);
    [_window addSubview:button];
    [button addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
    // 给button添加文字
    [button setTitle:@"点我啊" forState:UIControlStateNormal];
    // 修改button的颜色
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    // UITextField
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 400, 100, 40)];
    textField.backgroundColor = [UIColor redColor];
    [_window addSubview:textField];
    // 给输入框赋了初值
    textField.text = @"文字";
    // 文字颜色
    textField.textColor = [UIColor blueColor];
    // boderStyle 边缘样式:
    // UITextBorderStyleNone 无
    // UITextBorderStyleLine 有边缘线
    // UITextBorderStyleRoundedRect 边缘圆角
    textField.borderStyle = UITextBorderStyleLine;
    // 占位字符 当text不为空时, placehoder是不显示的
    textField.placeholder = @"请输入..";
    textField.clearsOnBeginEditing = YES;
    // command + K 显示/收起键盘
    textField.delegate = self;
    
    return YES;
}
// 开始编辑之前, 判断能否进行编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    // return  YES 可以编辑 默认
    // return NO 不可以编辑
    return YES;
}
// 已经开始编辑时
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    
}
// 判断是否结束编辑
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    return YES;
}
// 已经结束编辑
- (void)textFieldDidEndEditing:(UITextField *)textField {
    
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    // 处理点击retur, 需要进行的操作
    // 回收键盘
    [textField endEditing:YES];
    return YES;
}
- (void)buttonClicked {
    // 结束编辑
    [_window endEditing:YES];
    NSLog(@"我被点了");
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
